package com.wswy.wzcx.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.liulishuo.okdownload.DownloadTask;
import com.liulishuo.okdownload.SpeedCalculator;
import com.liulishuo.okdownload.core.breakpoint.BlockInfo;
import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo;
import com.liulishuo.okdownload.core.cause.EndCause;
import com.liulishuo.okdownload.core.listener.DownloadListener4WithSpeed;
import com.liulishuo.okdownload.core.listener.assist.Listener4SpeedAssistExtend;
import com.wswy.wzcx.R;
import java.io.File;
import java.util.List;
import java.util.Map;

public class DownloadNotificationListener extends DownloadListener4WithSpeed {

  private int totalLength;

  private NotificationCompat.Builder builder;
  private NotificationManager manager;
  private DownloadCallback taskEndRunnable;
  private Context context;

  private NotificationCompat.Action action;

  public DownloadNotificationListener(Context context) {
    this.context = context.getApplicationContext();
  }

  public void attachTaskEndRunnable(DownloadCallback taskEndRunnable) {
    this.taskEndRunnable = taskEndRunnable;
  }

  public void releaseTaskEndRunnable() {
    taskEndRunnable = null;
  }

  public void setAction(NotificationCompat.Action action) {
    this.action = action;
  }

  public void initNotification(String title, PendingIntent pendingIntent) {
    manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    final String channelId = "okdownload";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      final NotificationChannel channel = new NotificationChannel(
          channelId,
          "下载",
          NotificationManager.IMPORTANCE_MIN);
      manager.createNotificationChannel(channel);
    }

    builder = new NotificationCompat.Builder(context, channelId);

    builder.setDefaults(Notification.DEFAULT_LIGHTS)
        .setOngoing(true)
        .setOnlyAlertOnce(true)
        .setPriority(NotificationCompat.PRIORITY_MIN)
        .setContentTitle(title)
        .setContentIntent(pendingIntent)
        //.setContentText("Download a task showing on notification sample")
        .setSmallIcon(R.drawable.umeng_push_notification_default_small_icon);

    if (action != null) {
      builder.addAction(action);
    }
  }

  @Override
  public void taskStart(@NonNull DownloadTask task) {
    builder.setTicker("taskStart");
    builder.setOngoing(true);
    builder.setAutoCancel(false);
    //builder.setContentText("The task is started");
    builder.setProgress(0, 0, true);
    manager.notify(task.getId(), builder.build());
  }

  @Override
  public void connectStart(@NonNull DownloadTask task, int blockIndex,
      @NonNull Map<String, List<String>> requestHeaderFields) {
    builder.setTicker("connectStart");
//        builder.setContentText(
//                "The connect of " + blockIndex + " block for this task is connecting");
    builder.setProgress(0, 0, true);
    manager.notify(task.getId(), builder.build());
  }

  @Override
  public void connectEnd(@NonNull DownloadTask task, int blockIndex, int responseCode,
      @NonNull Map<String, List<String>> responseHeaderFields) {
    builder.setTicker("connectStart");
//        builder.setContentText(
//                "The connect of " + blockIndex + " block for this task is connected");
    builder.setProgress(0, 0, true);
    manager.notify(task.getId(), builder.build());
  }

  @Override
  public void infoReady(@NonNull DownloadTask task, @NonNull BreakpointInfo info,
      boolean fromBreakpoint,
      @NonNull Listener4SpeedAssistExtend.Listener4SpeedModel model) {
    Log.d("NotificationActivity", "infoReady " + info + " " + fromBreakpoint);

    if (fromBreakpoint) {
      builder.setTicker("fromBreakpoint");
    } else {
      builder.setTicker("fromBeginning");
    }
//        builder.setContentText(
//                "This task is download fromBreakpoint[" + fromBreakpoint + "]");
    builder.setProgress((int) info.getTotalLength(), (int) info.getTotalOffset(), true);
    manager.notify(task.getId(), builder.build());

    totalLength = (int) info.getTotalLength();
  }

  @Override
  public void progressBlock(@NonNull DownloadTask task, int blockIndex,
      long currentBlockOffset,
      @NonNull SpeedCalculator blockSpeed) {
  }

  @Override
  public void progress(@NonNull DownloadTask task, long currentOffset,
      @NonNull SpeedCalculator taskSpeed) {
    Log.d("NotificationActivity", "progress " + currentOffset);

    //builder.setContentText("downloading with speed: " + taskSpeed.speed());
    builder.setProgress(totalLength, (int) currentOffset, false);
    manager.notify(task.getId(), builder.build());
  }

  @Override
  public void blockEnd(@NonNull DownloadTask task, int blockIndex, BlockInfo info,
      @NonNull SpeedCalculator blockSpeed) {
  }

  @Override
  public void taskEnd(@NonNull final DownloadTask task, @NonNull final EndCause cause,
      @Nullable Exception realCause,
      @NonNull SpeedCalculator taskSpeed) {
    builder.setOngoing(false);
    builder.setAutoCancel(true);

    builder.setTicker("taskEnd " + cause);
//        builder.setContentText(
//                "task end " + cause + " average speed: " + taskSpeed.averageSpeed());
    if (cause == EndCause.COMPLETED) {
      builder.setProgress(1, 1, false);
    }

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
      @Override
      public void run() {
        if (taskEndRunnable != null) {
          taskEndRunnable.onEnd(cause == EndCause.COMPLETED, task.getFile());
        }
        if (cause == EndCause.CANCELED) {
          manager.cancel(task.getId());
        } else {
          manager.notify(task.getId(), builder.build());
        }

      }
      // because of on some android phone too frequency notify for same id would be
      // ignored.
    }, 100);
  }


  public interface DownloadCallback {

    void onEnd(boolean complete, File file);
  }

}